package uiLayer;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTabbedPane;

import java.awt.GridLayout;

import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JCheckBox;

import modelLayer.OrderLine;
import modelLayer.SalesOrder;
import controlLayer.OrderCtr;
import dbLayer.DBSalesOrder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.FlowLayout;
import java.util.List;
import java.util.Date;

public class SaleTab3 extends JPanel {
	private JTextField txtDate;
	private JTextField txtName;
	private JTextField txtAddress;
	private JTextField txtZipCode;
	private JTextField txtCity;
	private JTextField txtPhone;
	private JTextField txtTotal;
	private OrderCtr orderCtr;
	private OrderLineTable orderLineTable;
	private DBSalesOrder dbSalesOrder;
	private List<OrderLineWrapper> orderLines;
	

	/**
	 * Create the panel.
	 * @param orderCtr 
	 */
	public SaleTab3(List<OrderLineWrapper> orderLines, OrderCtr orderCtr) {
		this.setOrderLines(orderLines);
		this.setOrderCtr(orderCtr);
		orderLineTable = new OrderLineTable();
		
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel topField = new JPanel();
		topField.setBorder(new TitledBorder(new EmptyBorder(5, 5, 5, 5), "Kundeoplysninger", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.add(topField, BorderLayout.NORTH);
		topField.setLayout(new BorderLayout(0, 0));
		
		JPanel searchField = new JPanel();
		searchField.setBorder(new EmptyBorder(5, 5, 5, 5));
		topField.add(searchField, BorderLayout.CENTER);
		searchField.setLayout(new GridLayout(5, 2, 0, 0));
		
		JLabel lblName = new JLabel("Navn:");
		searchField.add(lblName);
		
		txtName = new JTextField();
		txtName.setEditable(false);
		searchField.add(txtName);
		txtName.setColumns(10);
		
		JLabel lblAddress = new JLabel("Addresse:");
		searchField.add(lblAddress);
		
		txtAddress = new JTextField();
		txtAddress.setEditable(false);
		searchField.add(txtAddress);
		txtAddress.setColumns(10);
		
		JLabel lblZipCode = new JLabel("Postnr.:");
		searchField.add(lblZipCode);
		
		txtZipCode = new JTextField();
		txtZipCode.setEditable(false);
		searchField.add(txtZipCode);
		txtZipCode.setColumns(10);
		
		JLabel lblCity = new JLabel("By:");
		searchField.add(lblCity);
		
		txtCity = new JTextField();
		txtCity.setEditable(false);
		searchField.add(txtCity);
		txtCity.setColumns(10);
		
		JLabel lblPhone = new JLabel("Tlf:");
		searchField.add(lblPhone);
		
		txtPhone = new JTextField();
		txtPhone.setEditable(false);
		searchField.add(txtPhone);
		txtPhone.setColumns(10);
		
		JPanel dateField = new JPanel();
		dateField.setBorder(new EmptyBorder(5, 5, 5, 5));
		topField.add(dateField, BorderLayout.EAST);
		dateField.setLayout(new BorderLayout(0, 0));
		
		txtDate = new JTextField();
		txtDate.setText("" + new Date());
		txtDate.setEditable(false);
		dateField.add(txtDate, BorderLayout.NORTH);
		txtDate.setColumns(10);
		
		JPanel buttonPanel = new JPanel();
		panel.add(buttonPanel, BorderLayout.SOUTH);
		buttonPanel.setLayout(new BorderLayout(0, 0));
		
		JPanel placePanel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) placePanel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		placePanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		buttonPanel.add(placePanel, BorderLayout.EAST);
		
		JButton btnCancel = new JButton("Annuller");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				handleClickedCancel();
			}
		});
		placePanel.add(btnCancel);
		
		JButton btnBack = new JButton("Tilbage");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				handleClickedBack();
			}
		});
		placePanel.add(btnBack);
		
		JButton btnFinish = new JButton("Afslut");
		btnFinish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				handleClickedFinish();
			}
		});
		placePanel.add(btnFinish);
		
		JPanel paymentOptions = new JPanel();
		buttonPanel.add(paymentOptions, BorderLayout.WEST);
		paymentOptions.setLayout(new BorderLayout(0, 0));
		
		JPanel payment = new JPanel();
		payment.setBorder(new TitledBorder(new EmptyBorder(5, 5, 5, 5), "Betaling:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		paymentOptions.add(payment, BorderLayout.CENTER);
		payment.setLayout(new GridLayout(3, 2, 0, 0));
		
		JLabel lblTotal = new JLabel("Total:");
		payment.add(lblTotal);
		
		txtTotal = new JTextField();
		txtTotal.setEditable(false);
		payment.add(txtTotal);
		txtTotal.setColumns(10);
		
		JLabel lblCashCard = new JLabel("Kontant/kort");
		payment.add(lblCashCard);
		
		JCheckBox chckbxCashCard = new JCheckBox("");
		payment.add(chckbxCashCard);
		
		JLabel lblAccount = new JLabel("Tilf�j til konto");
		payment.add(lblAccount);
		
		JCheckBox chckbxAccount = new JCheckBox("");
		payment.add(chckbxAccount);
		
		JPanel contentPanel = new JPanel();
		panel.add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		contentPanel.add(scrollPane);
		
		this.addComponentListener(new ComponentListener() {
			
			@Override
			public void componentShown(ComponentEvent arg0) {
				setTableModel();
				calculateTotal();
				importCustomer();
			}
			
			@Override
			public void componentResized(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentMoved(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentHidden(ComponentEvent arg0) {
				setTableModel();
				
			}
		});
			
		
		scrollPane.setViewportView(orderLineTable);

	}

	private void setTableModel() {
		List<OrderLine> orderLines = orderCtr.getSalesOrder().getOrderLines();
		System.out.println(orderLines.size());
		orderLineTable.setModel(orderLines);
	}

	protected void handleClickedBack() {
		JTabbedPane jtp = (JTabbedPane)this.getParent();
		jtp.setSelectedIndex(jtp.getSelectedIndex() - 1);
	}
	
	protected void handleClickedFinish() {
		if (orderCtr.getSalesOrder().getCustomer() != null){
			orderCtr.endOrder();
			clearInformation();
			moveBack();
		}
		else JOptionPane.showMessageDialog(this, "Du skal tilføje en kunde til ordren.", "Error : 40", JOptionPane.OK_OPTION);
	}

	protected void handleClickedCancel() {
		clearInformation();
		moveBack();
	}

	private void moveBack() {
		SaleTabbedPane saleTabbedPane = (SaleTabbedPane) this.getParent();
		saleTabbedPane.setOrderCtr(new OrderCtr());
		clearOrderLinesList();
		List<OrderLine> orderLines = orderCtr.getSalesOrder().getOrderLines();
		orderLines.clear();
		orderLineTable.setModel(orderLines);
		saleTabbedPane.setSelectedIndex(0);
	}

	/**
	 * @return the orderCtr
	 */
	public OrderCtr getOrderCtr() {
		return orderCtr;
	}

	/**
	 * @param orderCtr the orderCtr to set
	 */
	private void setOrderCtr(OrderCtr orderCtr) {
		this.orderCtr = orderCtr;
	}
	
	public void importCustomer() {
		if (orderCtr.getSalesOrder().getCustomer() != null){
			txtName.setText(orderCtr.getSalesOrder().getCustomer().getName());
			txtAddress.setText(orderCtr.getSalesOrder().getCustomer().getAddress());
			txtZipCode.setText("" + orderCtr.getSalesOrder().getCustomer().getZipCode());
			txtCity.setText(orderCtr.getSalesOrder().getCustomer().getCity());
			txtPhone.setText(orderCtr.getSalesOrder().getCustomer().getPhoneNo());
		}
		
	}
	
	private void calculateTotal() {
		double totalAmount = 0;
		List<OrderLine> orderLines = orderCtr.getSalesOrder().getOrderLines();
		int size = orderLines.size();
		//System.out.println(orderLines.get(0).getAmount());
		for (int orderRows = 0; orderRows < size; orderRows++) {
			totalAmount = ( orderLines.get(orderRows).getAmount() * orderLines.get(orderRows).getProduct().getSalesPrice() ) + totalAmount;
		}
		txtTotal.setText("" + totalAmount);
	}
	
	private void clearInformation() {
		txtName.setText("");
		txtAddress.setText("");
		txtZipCode.setText("");
		txtCity.setText("");
		txtPhone.setText("");
		txtTotal.setText("");
	}
	

	private void clearOrderLinesList() {
		orderLines.removeAll(orderLines);
	}

	/**
	 * @return the orderLines
	 */
	public List<OrderLineWrapper> getOrderLines() {
		return orderLines;
	}

	/**
	 * @param orderLines the orderLines to set
	 */
	public void setOrderLines(List<OrderLineWrapper> orderLines) {
		this.orderLines = orderLines;
	}
}
